package com.renwei.yunlong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.bean.ServiceContentsBean;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;

import java.util.List;

/**
 * 选择所属服务目录 adapter
 *
 * Created by Administrator on 2017/8/31.
 */

public class ServiceContentsAdapter extends RecyclerView.Adapter<ServiceContentsAdapter.ServiceContentsViewHolder>{

    private LayoutInflater inflater;
    private Context context;
    private List<ServiceContentsBean.Rows> mList;

    private int selectedPosition = -1;//记录

    private OnItemClickListener mOnItemClickListener;

    public ServiceContentsAdapter(Context context, List<ServiceContentsBean.Rows> mList){
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.mList = mList;
    }

    @Override
    public ServiceContentsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =  inflater.inflate(R.layout.item_select_belongs,null);
        AutoUtils.auto(view);
        return new ServiceContentsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ServiceContentsViewHolder holder, int position) {
        final ServiceContentsBean.Rows rows = mList.get(position);
        holder.tvTitle.setText(rows.getDetailName());
        if (selectedPosition != -1){
            if (selectedPosition == position){
                //选中
                holder.ivChosen.setVisibility(View.VISIBLE);
            }else {
                holder.ivChosen.setVisibility(View.GONE);
            }
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int pos = holder.getLayoutPosition();
                mOnItemClickListener.onItemClick(holder.itemView,pos,rows.getDetailName(),rows.getCompleteTimes(),rows.getType(),rows.getDetailId(),rows.getRelationId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null?0:mList.size();
    }

    public void setSelection(int position) {
        selectedPosition = position;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(View view, int position, String contractName,String completeTimes,String type,String detailId,String relationId);
    }

    static class ServiceContentsViewHolder extends RecyclerView.ViewHolder{
        private View itemView;
        private TextView tvTitle;//标题
        private ImageView ivChosen;//对勾

        public ServiceContentsViewHolder(View view) {
            super(view);
            itemView = view;
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            ivChosen = (ImageView) itemView.findViewById(R.id.iv_chosen);
        }
    }

}
